const global = getApp().globalData;
const wxApi = global.wxApi;
const Api = global.Api;
const util = global.util;
const getUserInfo = global.getUserInfo;
const drawQrcode = require('../../utils/qrcode.min.js');
Page({
  data: {
    screenHeight: "",
    tabValue: 1,
    invitedRecords: []
  },
  onLoad() {
    let userData = getUserInfo();
    let userPhone = userData.encryptedPhone;
    this.userPhone = userPhone;
    wxApi.setNavTitle('邀请好友');
    wxApi.getScreenSize(size => {
      this.setData({
        screenHeight: size.height
      })
    });
    this.loadInviteRecords(1, records => {
      console.log( records );
      this.setData({
        invitedRecords: records
      })
    } );
    this.drawQrcode();
  },
  drawQrcode() {
    let url = 'https://ia.jydsapp.com/dfc/register.html?' + this.userPhone;
    drawQrcode({
      width: 110,
      height: 110,
      canvasId: 'myQrcode',
      typeNumber: -1,
      text: url,
      callback(e) {
        console.log('e: ', e)
      }
    })
  },
  onShare() {
    wx.showShareMenu({
      success() {
        console.log( '.....' )
      }
    })
  },
  onShareAppMessage( res ) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '邀你一起高效学习',
      path: '/pages/index/index?userPhone=' + this.userPhone,
      imageUrl: 'https://omj0etkcd.qnssl.com/appBanner/banner3.jpg',
      success() {
        console.log( '分享成功' );
      },
      fail() {
        console.log( 'fail' )
      }
    }
  },
  inviteFriends() {
    this.setData({
      tabValue: 1
    })
  },
  inviteRecords() {
    this.setData({
      tabValue: 2
    })
  },
  loadInviteRecords(pageNo, callback) {
    let params = {
      pageNo,
      pageSize: 20
    }
    Api.inviteRecords(params, res => {
      let records = this.recordsDateFormat( res.records )
      callback( records )
    })
  },
  recordsDateFormat( records ) {
    let newRecords = [];
    for ( let i = 0, len = records.length; i < len; i++ ) {
      let record = records[ i ];
      record.dateTime = util.dateFormat("yyyy年MM月dd日", record.registerTime);
      newRecords.push( record );
    } 
    return newRecords;
  }
})